package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: qingtao
 * @Date 2022/3/10 9:01
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired(required = false)
    private CategoryService categoryService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 获得session
     * @return
     */
    public Long getUserId(){
       return  (Long)session.getAttribute("employee");
    }

    @RequestMapping("list")
    public R<List<Category>> findAllByType(Integer type){
        List<Category> allByType = categoryService.findAllByType(type);
        return R.success(allByType);
    }

    /**
     * 添加
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        category.setCreateUser(getUserId());
        category.setUpdateUser(getUserId());
        boolean save = categoryService.save(category);
        if (save){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("page")
    public R<Page<Category>> findByPage(Integer page,Integer pageSize){
        Page<Category> byPage = categoryService.findByPage(page, pageSize);
        return R.success(byPage);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        if (categoryService.delete(id)){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        category.setUpdateUser(getUserId());
        if (categoryService.update(category)){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    /**
     * 导出表格
     * @param response
     * @throws IOException
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //通过下载模式获得
        response.setHeader("content-disposition","attachment;filename=category.xlsx");
        //读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");
        // 使用模板的输入流构建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);
        //得到工作单
        Sheet sheet = workbook.getSheetAt(0);
        //4读取第一行,提取第一行的样式，并且存储到集合中
        Row row = sheet.getRow(1);
        List<CellStyle> cellStylesList= new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            //得到单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStylesList.add(cellStyle);
        }

        //获得数据
        List<Category> list = categoryService.findAll();

        //循环写入数据
        for (int i = 0; i < list.size(); i++) {
            //创建行排除头部
            row = sheet.createRow(i+1);
            Category category = list.get(i);

            //创建第一个列
            Cell cell = row.createCell(0);
            //数据
            cell.setCellValue(category.getType());
            //样式
            cell.setCellStyle(cellStylesList.get(0));

            cell = row.createCell(1);
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStylesList.get(1));

            cell = row.createCell(2);
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStylesList.get(2));

            cell = row.createCell(3);
            cell.setCellValue(category.getCreateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(3));

            cell = row.createCell(4);
            cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(4));
        }
        workbook.write(response.getOutputStream());
    }


}
